<?php
	require_once "../include/init.php";
	//检测安装状态
	if(is_file($Config['InstallTag'])){
		echo '<meta charset="utf-8">';
		echo '系统检测本机已经安装，如果需要重新安装，请删除install目录下的'.$Config['InstallTag'].'文件，<a href="../index">返回</a>';
		die;
	}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title><?php echo $Siteinfo['SiteName'];?>安装程序</title>
		<link href="../resource/plugin/jqui/swanky-purse/jquery-ui.min.css" rel="stylesheet">
		<link href="../resource/plugin/jquery.mCustomScrollbar/jquery.mCustomScrollbar.min.css" rel="stylesheet">
		<style>
		
		body{font: 75% "微软雅黑", "宋体";margin: 50px; background: #e1e1e1; margin:0px 30px; padding-top:0px;}

		.demoHeaders {margin-top: 2em;}
		
		select {width: 200px;}
		
		input.text { margin-bottom:12px; width:30%; padding: .4em; }

		.toggler {width: 500px;height: 200px;}
		
		#effect {position: relative;width: 240px;height: 135px;padding: 0.4em;}

		#effect h3 {margin: 0;padding: 0.4em;text-align: center;}
		
		#selectable .ui-selecting { background: #FECA40;}
		#selectable .ui-selected { background: #F39814; color: white; }
		#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
		#selectable li { width:25%; margin: 3px; padding: 0.4em; font-size: 1em; height: 18px; float:left;}
		
		.ui-progressbar {
			left:25%;
			top:50%;
			width:50%;
			z-index:999999;
			position: fixed;
		}
		.progress-label {
			position: absolute;
			left: 50%;
			top: 4px;
			font-weight: bold;
			text-shadow: 1px 1px 0 #fff;
		}
		</style>
	</head>
	<body>
		<div id="install" align="center">
			<h2 class="demoHeaders" align="left"><?php echo $Siteinfo['SiteName'];?>安装程序　当前版本：<?php echo $Siteinfo['Ver'];?></h2>
			<div id="tabs">
				<ul>
					<li><a href="#tabs-1">安装协议</a></li>
					<li><a href="#tabs-2" id="install_db" onclick="return false;">安装数据库</a></li>
					<li><a href="#tabs-3" id="config_website" onclick="return false;">配置网站</a></li>
				</ul>
				<!-- 安装协议 -->
				<div id="tabs-1" align="left"><div class="ui-corner-all" style="padding:5px 10px;font-size:1.2em;" id="install_pact"><?php require_once 'install_pact.php';?></div><br /><button id="button" onclick="$('#install_db').trigger('click');">同意并接受以上条款</button></div>
				<!-- 安装数据库 -->
				<div id="tabs-2" align="left">
					<input type="text" name="dbhost" id="dbhost" placeholder="数据库连接地址" class="text ui-widget-content ui-corner-all" value="127.0.0.1">
					<br />
					<input type="text" name="dbport" id="dbport" placeholder="端口号" class="text ui-widget-content ui-corner-all" value="3306">
					<br />
					<input type="text" name="dbuser" id="dbuser" placeholder="用户名" class="text ui-widget-content ui-corner-all" value="root">
					<br />
					<input type="password" name="dbpwd" id="dbpwd" placeholder="密码" class="text ui-widget-content ui-corner-all" value="root">
					<br />
					<div style="width:100%;">
						<ul style="width:200px;float:left;text-align:left;padding-left:0px;" id="selectUl">
							<select id="selectdb" name="selectdb">
								<option value="">选择附加数据库</option>
							</select>
						</ul>
						<ul style="float:left;padding-left:10px;">
							<button id="testconnectbutton">测试连接</button>
						</ul>
					</div>
					<div style="clear:both;"></div>
					<br />
					<input type="text" name="dbpre" id="dbpre" placeholder="表前缀" class="text ui-widget-content ui-corner-all" value="pre_">
					<br />
					<button id="installbutton">下一步</button>
				</div>
				<!-- 配置网站参数 -->
				<div id="tabs-3" align="left" style="height:300px;">
					<p>拖拽或按住Ctrl多选</p>
					<input type="hidden" id="module" name="module"><span id="select-result" style="display:none;"></span>
					<ol id="selectable">
						<li class="ui-widget-content">卡券模块</li>
						<li class="ui-widget-content">秒杀模块</li>
						<li class="ui-widget-content">拍卖模块</li>
						<li class="ui-widget-content">拼团模块</li>
						<li class="ui-widget-content">比价模块</li>
						<li class="ui-widget-content">文章模块</li>
						<li class="ui-widget-content">公告模块</li>
						<li class="ui-widget-content">采集文章</li>
						<li class="ui-widget-content">采集商品</li>
					</ol>
					<div style="clear:both;"></div>
					<br />
					<input type="text" name="adminname" id="adminname" placeholder="管理员用户名" class="text ui-widget-content ui-corner-all" value="">
					<br />
					<input type="text" name="adminpwd" id="adminpwd" placeholder="管理员密码" class="text ui-widget-content ui-corner-all" value="">
					<br />
					<button id="successbutton">安装</button>
					<br />
				</div>
			</div>
		</div>
		
		<div id="dialog-message" title="提示栏" style="display:none;">
			<p id="dialog_msg"></p>
		</div>
		
		<div id="dialog-confirm" title="确认栏" style="display:none;">
			<p id="dialog_confirm_msg"></p>
		</div>
		
		<div id="progressbar" style="display:none;"><div class="progress-label">准备中...</div></div>
		
		<div id="copyright" style="text-align:center;padding-top:30px;font-size:1.2em;">©CopyRight 2016-<?php echo date("Y");?> <?php echo $Siteinfo['Company'];?> 版权所有</div>
	</body>
	<script src="../resource/js/jquery-1.12.4.min.js"></script>
	<script src="../resource/js/common.js"></script>
	<script src="../resource/plugin/jqui/jquery-ui.min.js"></script>
	<script src="../resource/plugin/jquery.mCustomScrollbar/jquery.mCustomScrollbar.concat.min.js"></script>
	<script>
		function dialog(str,bool){
			if(bool){
				var style = "ui-icon ui-icon-circle-check";
			}else{
				var style = "ui-icon ui-icon-alert";	
			}
			$("#dialog-message").show();
			$("#dialog_msg").html('<span class="'+style+'" style="float:left; margin:0 7px 20px 0;"></span>'+str+'');
			$( "#dialog-message" ).dialog({
				modal: true,
				buttons: {
					"知道了": function() {
						$( this ).dialog( "close" );
					}
				}
			});			
		}

	    (function($){

	        $(window).load(function(){
	            $("#install_pact").mCustomScrollbar(
	            	{
	            		setHeight:360,
	            		theme:"3d-thick-dark"
	            	}
	            );
	        });
	        
			function progress() {
				var val = progressbar.progressbar( "value" ) || 0;
				progressbar.progressbar( "value", val + 2 );
				if ( val < 99 ) {
					setTimeout( progress, 80 );
				}
			}
			
			var progressbar = $( "#progressbar" ),
				progressLabel = $( ".progress-label" );
			progressbar.progressbar({
				value: false,
				change: function() {
					progressLabel.text( progressbar.progressbar( "value" ) + "%" );
				},
				complete: function() {
					progressLabel.text( "安装完成!" );
					var sleepDo = function(){
						$("#progressbar").css("display","none");
					}
					setTimeout(sleepDo,1500);
					href('../index');
				}
			});

			$( "#selectable" ).selectable({
				stop: function() {
					var result = $( "#select-result" ).empty();
					$( ".ui-selected", this ).each(function() {
						var index = $( "#selectable li" ).index( this );
						result.append( ( index + 1 ) + ',' );
					});
					var moduleval = $("#select-result").html();
					moduleval=moduleval.substring(0,moduleval.length-1);
					$("#module").val(moduleval);
				}
			});
			
			//安装
			$("#successbutton").click(function(){
				$("#dialog_confirm_msg").html("确定安装本程序吗");
				$( "#dialog-confirm" ).dialog({
					resizable: false,
					height:175,
					modal: true,
					buttons: {
						"确定": function() {
							var module = $("#module").val();	//模块					
							var dbhost = $("#dbhost").val();
							var dbport = $("#dbport").val();
							var dbuser = $("#dbuser").val();
							var dbpwd = $("#dbpwd").val();
							var selectdb = $("#selectdb").val();
							var adminname = $("#adminname").val();
							var adminpwd = $("#adminpwd").val();
							if(checkVal(dbhost) && checkVal(dbport) && checkVal(dbuser) && checkVal(dbpwd) && checkVal(selectdb) && checkVal(module) && checkVal(adminname) && checkVal(adminpwd)){
								$.post("process/install/1/2/3/",{dbhost:dbhost,dbport:dbport,dbuser:dbuser,dbpwd:dbpwd,selectdb:selectdb,adminpwd:adminpwd,adminname:adminname,module:module},function(data){
									if(data=='success'){
										$("#progressbar").css("display","block");
										setTimeout( progress, 1000 );
										$("#successbutton").attr("disabled","disabled");
									}else{
										dialog("安装失败",false);
									}
								});
							}else{
								dialog("请检查表单项目是否为空",false);
							}
							$( this ).dialog( "close" );
							return false;
						},
						"取消": function() {
							$( this ).dialog( "close" );
							return false;
						}
					}
				});
			});
	        
	        //下一步
	        $("#installbutton").click(function(){
				var dbhost = $("#dbhost").val();
				var dbport = $("#dbport").val();
				var dbuser = $("#dbuser").val();
				var dbpwd = $("#dbpwd").val();
				var selectdb = $("#selectdb").val();
				if(checkVal(dbhost) && checkVal(dbport) && checkVal(dbuser) && checkVal(dbpwd) && checkVal(selectdb)){
					$('#config_website').trigger('click');
				}else{
					dialog("请检查表单项目是否为空",false);
					return false;
				}
	        });
	        
			//测试连接
	        $("#testconnectbutton").click(function(){
				var dbhost = $("#dbhost").val();
				var dbport = $("#dbport").val();
				var dbuser = $("#dbuser").val();
				var dbpwd = $("#dbpwd").val();
				if(checkVal(dbhost) && checkVal(dbport) && checkVal(dbuser) && checkVal(dbpwd)){
					$.ajax({
				        url:"process/checkConnectDB/"+dbhost+"/"+dbport+"/"+dbuser+"/"+dbpwd+"",
				        data:{},
				        cache:false,
				        dataType:"json",
				        type:"GET",
				        async:true,
				        success:function(data){
				        	if(data.result=='success'){
								$("#selectUl").html('');
								var optionstr = '';
								$.each(data.content,function(i,j){
									optionstr+='<option value="'+j.table_schema+'">'+j.table_schema+'</option>';
								});
								resetSelect(optionstr);
								dialog("连接成功",true);
							}else{
								resetSelect('<option value="">选择附加数据库</option>');
				            	dialog("连接失败，请检查表单是否填写正确。",false);
							}
				            return false;
				        },
				        error:function(){
							resetSelect('<option value="">选择附加数据库</option>');
				            dialog("连接失败，请检查表单是否填写正确。",false);
				            return false;
				        }
				    });
				}else{
					resetSelect('<option value="">选择附加数据库</option>');
					dialog("请检查表单项目是否为空",false);
				}
				return false;
	        });
	    })(jQuery);
		
		$( "#button" ).button();
		$( "#installbutton" ).button();
		$( "#testconnectbutton" ).button();
		$( "#successbutton" ).button();
		$( "#radioset" ).buttonset();
		$( "#tabs" ).tabs();
		$( "#selectdb" ).selectmenu();
		
		var resetSelect = function(str){
			$("#selectUl").html('<select id="selectdb" name="selectdb">'+str+'</select>');
			$( "#selectdb" ).selectmenu();
		}
		
	</script>
</html>